package com.ciban.smarthome.module.ui;

import java.io.IOException;
import java.io.InputStream;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.framework.fund.widget.GestureContentView;
import com.ciban.smarthome.common.framework.fund.widget.GestureDrawline.GestureCallBack;
import com.ciban.smarthome.common.utils.DbUtils;
import com.ciban.smarthome.common.utils.FileUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.Constants;
import com.ciban.smarthome.module.db.dao.ConfigDao;

public class SplashActivity extends BaseActivity {
	private EditText et_pwd;
	private LinearLayout ll_show_pwd;
	private FrameLayout fl_fund;
	private Button bt_check;
	private SharedPreferences sp;
	private GestureContentView mGestureContentView;
	private String pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);

		initViews();

		initViewData();
	}

	private void initViewData() {

		int nowDbVersion=3;

		boolean isNeedCopyDb = true;

		if (FileUtils.isFileExists(Constants.DATABASE_PATH + "config")) {
			ConfigDao dao = new ConfigDao(this, "config");
			int version = dao.getDbVersion();

			LogUtil.info("当前数据库版本为：" + version);

			if (version >= nowDbVersion) {
				isNeedCopyDb = false;
			}
		}

		if (isNeedCopyDb) {
			AssetManager manager = getAssets();
			InputStream is;
			try {
				is = manager.open("config");
				DbUtils.copyDataBase(Constants.DATABASE_PATH, "config", is);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		sp = PreferenceManager.getDefaultSharedPreferences(this);

		int type = sp.getInt("pwd_type", 0);

		if (type > 0) {
			pwd = sp.getString("pwd_content", "");
		}

		if (type == 1) {
			ll_show_pwd.setVisibility(View.VISIBLE);

		} else if (type == 2) {

			showFundView();

		} else if (type == 0) {
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					goMainActivity();
				}
			}, 3000);
		}
	}

	private void checkPwd() {
		String pwds = et_pwd.getText().toString().trim();
		if (StringUtils.isEmpty(pwd)) {
			displayShort("密码不能为空");
			return;
		}
		if (pwd.equals(pwds)) {
			goMainActivity();
		} else {
			displayShort("密码错误");
		}

	}

	private void goMainActivity() {
		Intent intent = new Intent(SplashActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	private void initViews() {
		et_pwd = (EditText) findViewById(R.id.splash_pwd_input);
		ll_show_pwd = (LinearLayout) findViewById(R.id.splash_pwd_content);
		bt_check = (Button) findViewById(R.id.splash_pwd_check);
		fl_fund = (FrameLayout) findViewById(R.id.splash_pwd_content_fund);

		bt_check.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				checkPwd();
			}
		});
	}

	private void showFundView() {
		fl_fund.setVisibility(View.VISIBLE);
		mGestureContentView = new GestureContentView(this, true, pwd,
				new GestureCallBack() {

					@Override
					public void onGestureCodeInput(String inputCode) {

					}

					@Override
					public void checkedSuccess() {
						mGestureContentView.clearDrawlineState(0L);
						goMainActivity();
					}

					@Override
					public void checkedFail() {
						mGestureContentView.clearDrawlineState(1300L);
						Toast.makeText(SplashActivity.this, "密码错误", Toast.LENGTH_LONG)
								.show();
					}
				});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(fl_fund);
	}
}
